package com.liu.controller;

import com.liu.pojo.Novel;
import com.liu.pojo.Rate;
import com.liu.util.HttpServletFactory;
import com.liu.util.LoggerFactory;
import com.liu.util.Page;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 老刘Matthew
 * @Timestamp 2022/7/15 14:32
 * @Description
 */
@WebServlet("/comment/*")
public class CommentServlet extends GenericServlet implements HttpServletFactory, LoggerFactory {

    //小说详情评论查询
    public void _doQueryRates(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");

            Integer novel_id = Integer.parseInt(request.getParameter("novel_id"));
            List<Rate> list = rateDao.query(novel_id);

            String author_name = request.getParameter("author_name");
            List<Novel> novelList = novelDao.queryListByAuthor_name(author_name);  //调用查询小说详情方法
            if (list.size() > 0 && novelList.size() > 0) {
                request.setAttribute("rates", list);
                request.setAttribute("novelList", novelList);
            } else {
                request.setAttribute("msg", "暂无数据");
            }

            new GenericServlet.GenericServletBuilder(request, response, this.getClass())
                    .setUrl(Page.DETAIL.get())
                    .build()
                    .getRequestMapping();
            
        } catch (Exception e) {
            logger.error("评论查询异常！", e);
        }
    }

    //小说详情跳转发表评论传递参数
    public void _doPublisComment(HttpServletRequest request, HttpServletResponse response){
        try{
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");

            String author_name = request.getParameter("author_name");
            String user_id = request.getParameter("user_id");
            String novel_id = request.getParameter("novel_id");
            List<String> slist = new ArrayList<>();
            slist.add(user_id);
            slist.add(author_name);
            slist.add(novel_id);
            request.setAttribute("slist",slist);

            new GenericServlet.GenericServletBuilder(request, response, this.getClass())
                    .setUrl(Page.COMMENT.get())
                    .build()
                    .getRequestMapping();

        } catch (Exception e) {
            logger.error("转发评论界面异常！", e);
        }
    }
            
    //新增评论
    public void _doInsertCom(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");

            Integer novel_id = Integer.parseInt(request.getParameter("novel_id"));
            String content = request.getParameter("content");
            Integer user_id = Integer.parseInt(request.getParameter("user_id"));

            Rate o = new Rate();
            o.setNovel_id(novel_id);
            o.setContent(content);
            o.setUser_id(user_id);
            o.setPush_time(new Timestamp(System.currentTimeMillis()));

            Integer result = rateDao.save(o);

            if (result > 0) {
                request.setAttribute("msg", "评论成功");
            } else {
                request.setAttribute("msg", "评论失败错误");
            }

            new GenericServlet.GenericServletBuilder(request, response, this.getClass())
                    .setUrl(Page.DETAIL.get()) //
                    .build()
                    .getRequestMapping();

        } catch (Exception e) {
            logger.error("新增评论异常！", e);
        }
    }

    //回复评论
    public void _doupdateComment(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");

            Integer novel_id = Integer.parseInt(request.getParameter("novel_id"));
            String content = request.getParameter("content");
            Integer user_id = Integer.parseInt(request.getParameter("user_id"));
            Integer revert_id = Integer.parseInt(request.getParameter("revert_id"));

            Rate o = new Rate();
            o.setNovel_id(novel_id);
            o.setContent(content);
            o.setUser_id(user_id);
            o.setPush_time(new Timestamp(System.currentTimeMillis()));
            o.setRevert_id(revert_id);

            Integer result = rateDao.saveWithRevert_id(o);

            if (result > 0) {
                request.setAttribute("msg", "评论成功");
            } else {
                request.setAttribute("msg", "评论失败错误");
            }

            new GenericServlet.GenericServletBuilder(request, response, this.getClass())
                    .setUrl(Page.DETAIL.get())
                    .build()
                    .getRequestMapping();

        } catch (Exception e) {
            logger.error("回复评论异常！", e);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {

        try {

            new GenericServletBuilder(request, response, this.getClass()) //
                    .build()
                    .getRequestMapping();

        } catch (Exception e) {
            logger.error("控制转发异常！", e);
        }
    }

}
